home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 6853 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.1 KB  |  64 lines

  1. Path: sun001.spd.dsccc.com!spd!jmccarty
  2. From: jmccarty@spd.dsccc.com (Mike McCarty)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Confusion as to the proper use of MODULAS
  5. Date: 15 Feb 1996 19:24:27 GMT
  6. Organization: DSC Communications Corporation, Plano, Texas USA
  7. Message-ID: <4g019b$gvc@sun001.spd.dsccc.com>
  8. References: <4fr8be$ass@news.iconn.net> <31224679.6193@born.com> <4fthsu$1kl@ixnews7.ix.netcom.com>
  9. NNTP-Posting-Host: aplo139.spd.dsccc.com
  10.  
  11. In article <4fthsu$1kl@ixnews7.ix.netcom.com>, KPN  <wzjn@ix.netcom.com> wrote:
  12. )Confusion as to the proper use of MODULAS
  13. )
  14. )Very confused. Just as I think IÆve got it correct, I make-up a new
  15. )test, the results of which leave me mixed-up again.
  16. )
  17. )To begin with, this is the first rule I found out:
  18. )
  19. )Modulas operator simply shows:  *** AFTER *** you divide, what is the
  20. )remainder?
  21. )EXAMPLE of this:
  22. )    9 % 7 = ?
  23. )    9 divided by 7 yields a remainder of 2
  24. )    9 % 7 = 2
  25. )
  26. )1) MODULAS means divide a number and give me the left over number. OK -
  27. )not bad.
  28.  
  29. [snip]
  30.  
  31. )Here, begin my troubles: using MODULAS in an IF statement.
  32. )
  33. )I made up a test to see if a number I inputted was a 7. Sample code:
  34. )
  35. )   if (number % 7)
  36. )      printf("Not a 7\n");
  37. )   else
  38. )      printf("First integer was a 7\n");
  39. )
  40. )So, if the number WAS a 7, the ELSE would take over. OK. But, when I
  41. )enter in a ZERO, it still tells me that the number was a seven?
  42. )
  43. )Am I going in the right direction? Can someone tell me what IÆm doing
  44. )incorrectly, or where IÆm straying? WhatÆs the rule here for using MOD
  45. )in an IF statement?
  46.  
  47. You used it just fine. You just still don't exactly understand what a
  48. remainder is. You are testing for divisibility. Zero (0) is exactly
  49. divisible by 7 (or any other non-zero number, for that matter), with no
  50. remainder. In fact,
  51.  
  52.     0 = (0)(7)+(0)
  53.  
  54. And since |0| <= |0| (absolute of remainder less or equal to absolute of
  55. divisor), 0 is the remainder.
  56.  
  57. So 0 is a multiple of 7. So is 14. So 14 % 7 is 0.
  58.  
  59. Mike
  60. ----
  61. char *p="char *p=%c%s%c;main(){printf(p,34,p,34);}";main(){printf(p,34,p,34);}
  62.  
  63. I don't speak for DSC.         <- They make me say that.
  64.